<?php
namespace app\index\model;
use think\Db;
use think\Model;

class Search_model {
	public function getSearch() {
		$type = $_POST['type'];
		$title = $_POST['title'];
		$name = $_POST['name'];
		$start = $_POST['start'];
		$end = $_POST['end'];

		//文件夹还有问题  先写死

		if ($title) {
			if ($name) {
				if ($start && $end) {
					if ($type == 0) {
						$where = [
							'ca.arc_title' => ['like', '%' . $title . '%'],
							'ca.arc_name' => ['like', '%' . $name . '%'],
							'ci.update_time' => ['between', array($start, $end)],
							'ca.is_delete' => 0,
							'cac.cate_type' => 2,
							'ci.interfile_interfile_status' => 3,
						];
					} else {
						$where = [
							'ca.arc_title' => ['like', '%' . $title . '%'],
							'ca.arc_name' => ['like', '%' . $name . '%'],
							'ci.update_time' => ['between', array($start, $end)],
							'ca.is_delete' => 0,
							'cac.cate_type' => 2,
							'ca.mark' => $type,
							'ci.interfile_interfile_status' => 3,

						];
					}
				} else {
					if ($type == 0) {
						$where = [
							'ca.arc_title' => ['like', '%' . $title . '%'],
							'ca.arc_name' => ['like', '%' . $name . '%'],
							'ca.is_delete' => 0,
							'ci.interfile_status' => 3,

						];
					} else {
						$where = [
							'ca.arc_title' => ['like', '%' . $title . '%'],
							'ca.arc_name' => ['like', '%' . $name . '%'],
							'ca.is_delete' => 0,
							'cac.cate_type' => 2,
							'ca.mark' => $type,
							'ci.interfile_status' => 3,

						];

					}
				}
			} else {
				if ($start && $end) {
					if ($type == 0) {
						$where = [
							'ca.arc_title' => ['like', '%' . $title . '%'],
							'ci.update_time' => ['between', array($start, $end)],
							'ca.is_delete' => 0,
							'cac.cate_type' => 2,
							'ci.interfile_status' => 3,

						];
					} else {
						$where = [
							'ca.arc_title' => ['like', '%' . $title . '%'],
							'ci.update_time' => ['between', array($start, $end)],
							'ca.is_delete' => 0,
							'cac.cate_type' => 2,
							'ca.mark' => $type,
							'ci.interfile_status' => 3,

						];
					}
				} else {
					if ($type == 0) {
						$where = [
							'ca.arc_title' => ['like', '%' . $title . '%'],
							'ca.is_delete' => 0,
							'cac.cate_type' => 2,
							'ci.interfile_status' => 3,

						];
					} else {
						$where = [
							'ca.arc_title' => ['like', '%' . $title . '%'],
							'ca.is_delete' => 0,
							'cac.cate_type' => 2,
							'ca.mark' => $type,
							'ci.interfile_status' => 3,

						];
					}

				}
			}
		} else if ($name) {
			if ($start && $end) {
				if ($type == 0) {
					$where = [
						'ca.arc_name' => ['like', '%' . $name . '%'],
						'ci.update_time' => ['between', array($start, $end)],
						'ca.is_delete' => 0,
						'cac.cate_type' => 2,
						'ci.interfile_status' => 3,

					];
				} else {
					if ($type == 0) {
						$where = [
							'ca.arc_name' => ['like', '%' . $name . '%'],
							'ci.update_time' => ['between', array($start, $end)],
							'ca.is_delete' => 0,
							'cac.cate_type' => 2,
							'ci.interfile_status' => 3,

						];
					} else {
						$where = [
							'ca.arc_name' => ['like', '%' . $name . '%'],
							'ci.update_time' => ['between', array($start, $end)],
							'ca.is_delete' => 0,
							'cac.cate_type' => 2,
							'ca.mark' => $type,
							'ci.interfile_status' => 3,

						];
					}
				}
			} else {
				if ($type == 0) {
					$where = [
						'ca.arc_name' => ['like', '%' . $name . '%'],
						'ca.is_delete' => 0,
						'cac.cate_type' => 2,
						'ci.interfile_status' => 3,

					];
				} else {
					$where = [
						'ca.arc_name' => ['like', '%' . $name . '%'],
						'ca.is_delete' => 0,
						'cac.cate_type' => 2,
						'ca.mark' => $type,
						'ci.interfile_status' => 3,

					];
				}
			}
		} else if ($start && $end) {
			if ($type == 0) {
				$where = [
					'ci.update_time' => ['between', array($start, $end)],
					'ca.is_delete' => 0,
					'cac.cate_type' => 2,
					'ci.interfile_status' => 3,

				];
			} else {
				$where = [
					'ci.update_time' => ['between', array($start, $end)],
					'ca.is_delete' => 0,
					'cac.cate_type' => 2,
					'ca.mark' => $type,
					'ci.interfile_status' => 3,

				];
			}
		} else {
			if ($type == 0) {
				$where = [
					'ca.is_delete' => 0,
					'cac.cate_type' => 2,
					'ci.interfile_status' => 3,

				];
			} else {
				$where = [
					'ca.is_delete' => 0,
					'cac.cate_type' => 2,
					'ca.mark' => $type,
					'ci.interfile_status' => 3,

				];
			}
		}
		$res = Db::table('cn_archives')
			->alias('ca')
			->join('cn_archives_file caf', 'caf.arc_id = ca.arc_id')
			->join('cn_file_log cfl', 'caf.file_id = cfl.file_id')
			->join('cn_archives_category cac', 'cac.cate_id = ca.category_id')
			->join('cn_interfile ci', 'ci.arc_id = ca.arc_id')
			->where($where)
			->order('ci.update_time', 'asc')
			->field('ca.arc_id,arc_title,arc_name,arc_type,cfl.file_save_path,ca.mark')
			->select();
		// for ($i = 0; $i < count($res); $i++) {
		// 	$res[$i]['path'] = substr($res[$i]['file_save_path'], strpos($res[$i]['file_save_path'], '/folder'));
		// }
		if ($res) {
			return $res;
		} else {
			return false;
		}
		// }

	}

}

?>